package com.psc.view.utils;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

/**
 * Creator:Yx
 * Time:2020/8/20 11:34
 * Description:解决WebView 透明状态栏下，输入框遮挡问题
 * 解决透明状态栏下，弹出键盘遮挡输入框的问题：
 * 在onCreate里面加上：
 * WindowSoftModeAdjustResizeExecutor.assistActivity(this);
 */
public class WindowSoftModeAdjustResizeExecutor {

    public static void assistActivity(Activity activity) {
        new WindowSoftModeAdjustResizeExecutor(activity);
    }

    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private WindowSoftModeAdjustResizeExecutor(Activity activity) {
        //拿到当前activity.XML文件的根布局
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        //监听当前View的状态,进行通知回调,即"软键盘弹出""
        mChildOfContent = content.getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }

    /**
     * 重新设置高度
     * <p>
     * 把界面高度设置为可用高度
     */
    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            // int usableHeightSansKeyboard = activity.getWindowManager().getDefaultDisplay().getHeight();//获取屏幕尺寸，不包括虚拟功能高度 用这个可以完美解决
            //findViewById(android.R.id.content).getMeasuredHeight() 也可以解决虚拟按键问题
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();

            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            //frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;

            //排除其他View引起的变化,专注软键盘变化
            if (heightDifference > (usableHeightSansKeyboard / 4)) {
                // keyboard probably just became visible键盘可能刚刚出现
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden键盘可能只是藏起来了
                frameLayoutParams.height = usableHeightSansKeyboard;
            }

            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    /**
     * 软键盘弹出后,可以显示内容的高度
     *
     * @return
     */
    private int computeUsableHeight() {
        Rect r = new Rect();
        //这行代码能够获取到去除标题栏和被软键盘挡住的部分,所剩下的矩形区域
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        //小于5.0版本直接非全屏模式
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            return (r.bottom - r.top);
        }
        //r.top : 标题栏的高度
        //屏幕高度-r.bottom : 软键盘的高度
        //可用高度(全屏模式) : rect.bottom
        //可用高度(非全屏模式) : rect.bottom - rect.top
        return r.bottom;// 全屏模式下： return r.bottom
    }

}
